package com.zystudio.func.ui;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.zystudio.func.R;
import com.zystudio.func.interf.SingleClickListener;
import com.zystudio.func.net.ApiRequest;
import com.zystudio.func.net.IApiListener;
import com.zystudio.func.other.Const;

public class ApiActivity extends Activity {


    private RadioButton releaseRB;
    private Button onlineBtn, purBtn, ovmBtn;
    private TextView retTV;
    private EditText packET;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zy_func_api);
        bindView();
        bindEvent();
    }

    private void bindView() {
        releaseRB = findViewById(R.id.rb_pro);
        packET = findViewById(R.id.et_pack);
        onlineBtn = findViewById(R.id.btn_ly);
        purBtn = findViewById(R.id.btn_ml);
        ovmBtn = findViewById(R.id.btn_ovm);
        retTV = findViewById(R.id.tv_api_ret);
    }

    private void bindEvent() {
        //线上广告
        onlineBtn.setOnClickListener(new SingleClickListener() {
            @Override
            public void onSingleClick(View v) {
                startApi(0);
            }
        });
        //买量广告
        purBtn.setOnClickListener(new SingleClickListener() {
            @Override
            public void onSingleClick(View v) {
                startApi(1);
            }
        });
        //OVM广告
        ovmBtn.setOnClickListener(new SingleClickListener() {
            @Override
            public void onSingleClick(View v) {
                startApi(2);
            }
        });
    }

    private String environment() {

        return releaseRB.isChecked() ? Const.PRODUCTION : Const.SANDBOX;
    }

    private final Handler handler = new Handler(Looper.getMainLooper()) {

        @Override
        public void handleMessage(Message msg) {
            retTV.setText((String) msg.obj);
        }
    };

    private void startApi(int index) {

        String pack = packET.getText().toString().trim();
        if (TextUtils.isEmpty(pack)) {
            Toast.makeText(this, "请输入包名", Toast.LENGTH_SHORT).show();
            return;
        }
        retTV.setText("");
        String url = environment() + resource(index) + "?p=" + pack;
        ApiRequest.Builder builder = new ApiRequest.Builder(url);
        ApiRequest api = builder.build();
        api.executeGet(new IApiListener() {
            @Override
            public void onSuccess(String data) {
                Message message = Message.obtain();
                message.obj = data;
                handler.sendMessage(message);
            }

            @Override
            public void onFailed(int code, String msg) {
                Message message = Message.obtain();
                message.obj = msg;
                handler.sendMessage(message);
            }
        });
    }

    private String resource(int index) {

        switch (index) {
            case 0:
                return Const.PATH_ONLINE;
            case 1:
                return Const.PATH_PUR;
            case 2:
                return Const.PATH_OVM;
        }
        return null;
    }
}
